function getCookie(name) {
    var prefix = name + "="  // name=
    console.log(document.cookie) // name=wangwu; Hm_lvt_eaa57ca47dacb4ad4f5a2570
    // name=  第一次出现的位置
    var start = document.cookie.indexOf(prefix)
    if (start == -1) {
        return null;
    }
    // 从0+5这个位置开始，找到;第一次出现的位置，也就是这个cookie值得结束位置
    var end = document.cookie.indexOf(";", start + prefix.length)
    // name=wangwu
    // Hm_lvt_eaa57ca47dacb4ad4f5a2570;name=wangwu
    // 将字符串得总长度就是截取值得结束位置
    if (end == -1) { // 没有找到分号
        end = document.cookie.length;
    }
    var value = document.cookie.substring(start + prefix.length, end)
    return unescape(value);
}

var userId = getCookie("userid")

var phtot_path = "../img/couphoto/"

var currPage = 1

var pageSize = 5

var page

console.log(typeof pageSize)

var search = location.search

var arr = search.split("=")

if (arr.length > 1) {
    currPage = arr[1]
}

if (currPage > 0 && currPage < 100) {
    page = currPage;
} else {
    page = 1;
}


htmlStart(page, pageSize, userId)

function htmlStart(page, pageSize, userId) {
    $.ajax({
        url: "http://localhost:8080/user/delucoudate",
        data: "userId=" + userId,
        asyne: false,
        dataType: "json",
        type: "get",
        success: function (data) {
            if (data.code === 0) {
                nextHtml(page, pageSize, userId)
                layui.use(['form', 'layer', 'upload'], function () {
                    var layer = layui.layer;
                    layer.msg('亲，你有过期的优惠券，已经迷路了嗷')
                })
            } else if (data.code === 1) {
                nextHtml(page, pageSize, userId)
            } else {
                layui.use(['form', 'layer', 'upload'], function () {
                    // var form = layui.form;
                    var layer = layui.layer;
                    var upload = layui.upload;
                    var $ = layui.jquery;
                    layer.msg('好像加载失败了，正在殴打技术猛男解决')
                })
            }
        }
    })
}

function nextHtml(page, pageSize, userId) {
    $.ajax({
        url: "http://localhost:8080/user/selectUCoupon",
        data: "CHolding=" + 1 + "&currPage=" + page + "&pageSize=" + pageSize + "&userId=" + userId,
        asyne: false,
        dataType: "json",
        type: "get",
        success: function (data) {

            let html = ''
            var list = data.data.list

            for (let i = 0; i < list.length; i++) {
                html += `<div class="details" >
                        <img width="184px" height="100px" src="${phtot_path + list[i].couponMoney}.png" alt="" class="details_img">
                        <ul class="details_ul">
                            <li class="details_li_three">${list[i].usercouponDate}</li>
                            <li class="details_li_two">${list[i].couponMoney}</li>
                            <li class="details_li_one">${list[i].couponType}</li>
                        </ul>
                    </div>`
            }
            $(".mc_show").append(html)

            generatePage(data.data.navigatepageNums, data.data.pageNum, data.data.pages)
        }
    })
}

function generatePage(navigatepageNums, pageNum, pages) {
    var html = ''
    if (pageNum == 1) {
        html += `<nav class="lf_up"><a href="#" style="cursor: not-allowed">上一页</a></nav>`
    } else {
        html += `<nav class="lf_up"><a href="http://localhost:63343/qiguai/html/ucouhold.html?currPage=${pageNum - 1}">上一页</a></nav>`
    }

    for (let i = 0; i < navigatepageNums.length; i++) {
        if (pageNum == navigatepageNums[i]) {
            html += `<nav class="pagehave"><a href="#" class="pagehave">${navigatepageNums[i]}</a></nav>`
        } else {
            html += `<nav class="pagehave"><a href="http://localhost:63343/qiguai/html/ucouhold.html?currPage=${navigatepageNums[i]}" class="pagenone">${navigatepageNums[i]}</a></nav>`
        }
    }

    if (pageNum == pages) {
        html += `<nav class="lf_dowm"><a href="#" style="cursor: not-allowed">下一页</a></nav>`
    } else {
        html += `<nav class="lf_dowm"><a href="http://localhost:63343/qiguai/html/ucouhold.html?currPage=${pageNum + 1}">下一页</a></nav>`
    }
    $(".pagemain").append(html)
}